//
//  CourseDetailTopCell.m
//  TrainTree
//
//  Created by wanghui on 2017/9/13.
//  Copyright © 2017年 YouYeShu. All rights reserved.
//

#import "CourseDetailTopCell.h"
#import "CourseRequestManager.h"
@implementation CourseDetailTopCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.addBtn.layer.borderWidth = 1.0f;
    self.addBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    self.addBtn.layer.cornerRadius = 3.0f;
    self.addBtn.clipsToBounds = YES;

}

- (IBAction)addOrDeleteLesson:(id)sender {
        // self.status 0是未加入，1已加入; type 0添加 1 移除
    int type = 0;
    if ([self.status isEqualToString:@"0"]) {
        type = 0;
    } else {
        type = 1;
    }
    WeakSelf(ws);
    [CourseRequestManager addOrDeleteLessonWithLessonId:_lessonId withType:type responseBlock:^(id JSONData, NSError *error, NSURLSessionDataTask *dataTask) {
        if (JSONData) {

            if ([JSONData[@"c"] intValue] == 0) {
                ws.status = [NSString stringWithFormat:@"%d",type];
                if ([ws.status isEqualToString:@"0"]) {
                    ws.status = @"1";
                    if ([ws.status isEqualToString:@"1"]) {
                        [ws.addBtn setImage:ImageNamed(@"remove") forState:UIControlStateNormal];
                        ws.addBtn.backgroundColor = [UIColor whiteColor];
                        [ws.addBtn setTitle:@"移除课程表" forState:UIControlStateNormal];
                        [ws.addBtn setTitleColor: rgb(74.0f, 74.0f, 74.0f) forState:UIControlStateNormal];
                    }
                } else {
                    ws.status = @"0";
                    if ([ws.status isEqualToString:@"0"]) {
                        [ws.addBtn setImage:ImageNamed(@"add") forState:UIControlStateNormal];
                        ws.addBtn.backgroundColor = [UIColor clearColor];
                        [ws.addBtn setTitle:@"加入课程表" forState:UIControlStateNormal];
                        [ws.addBtn setTitleColor: [UIColor whiteColor] forState:UIControlStateNormal];
                    }
                }
            }else if ([JSONData[@"c"] intValue]== -1){
                [SVProgressHUD showErrorWithStatus:JSONData[@"m"]];
            }else{
                [SVProgressHUD showErrorWithStatus:JSONData[@"m"]];
            }

        }
    }];
}

- (void)configDataWithStatus: (NSString *)status classify:(NSString *)classify detailTitle:(NSString *)title imgUrl:(NSString *)url lessonId:(NSString *)lessonId studyType:(NSString *)studyType
{
    self.lessonId = lessonId;
    self.status = status;
    if ([self.status isEqualToString:@"0"]) {
        [self.addBtn setImage:ImageNamed(@"add") forState:UIControlStateNormal];
        self.addBtn.backgroundColor = [UIColor clearColor];
        [self.addBtn setTitle:@"加入课程表" forState:UIControlStateNormal];
        [self.addBtn setTitleColor: [UIColor whiteColor] forState:UIControlStateNormal];
    } else {
        if ([studyType isEqualToString:@"1"]) {//选修显示移除，必修不显示
            [self.addBtn setImage:ImageNamed(@"remove") forState:UIControlStateNormal];
            self.addBtn.backgroundColor = [UIColor whiteColor];
            [self.addBtn setTitle:@"移除课程表" forState:UIControlStateNormal];
            [self.addBtn setTitleColor:rgb(74.0f, 74.0f, 74.0f) forState:UIControlStateNormal];
        }else{// 必修直接隐藏按钮
            self.addBtn.hidden = YES;
        }


    }
    self.classify.text = classify;
    self.detailTitle.text = title;
//    [self.icon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",TrainTreeFileImgURL,url]] placeholderImage:ImageNamed(@"bear.jpg")];
    self.icon.image = ImageNamed(@"ty_pic");


}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
